﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://10.138.2.41:7001/sgpms/portal/default.jsp");
        }

        bool startLogin = false;
        public delegate void DG();
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
     
            if (webBrowser1.Url.ToString().Contains("http://10.138.2.41:7001/sgpms/portal/default.jsp"))
            {
                new Thread(delegate () {
                new AutoResetEvent(false).WaitOne(1000);
                DG dg = delegate () {
                var ele = webBrowser1.Document.GetElementById("userNameText");

                if (ele != null)
                {
                    startLogin = true;
                    ele.SetAttribute("value", "zhangyp1719");
                }
                ele = webBrowser1.Document.GetElementById("passwordText");

                if (ele != null)
                {
                    ele.SetAttribute("value", "XShy6789");
                    SendKeys.SendWait("{Enter}");

                        new Thread(delegate () {
                            DG d = delegate () {
                                new AutoResetEvent(false).WaitOne(10000);
                                ele = webBrowser1.Document.GetElementById("userNameText");

                                if (ele == null)
                                {
                                    MessageBox.Show("登陆成功");
                                }
                                else
                                {
                                    MessageBox.Show("登陆失败");
                                }
                                this.Dispose();
                            };

                            BeginInvoke(d);

                        }).Start();
                       
                    }
                    };

                    this.Invoke(dg);

                }).Start();



            }

        }

    }
}
